package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.entity.Result;
import com.atguigu.pojo.TravelGroup;
import com.atguigu.pojo.TravelItem;
import com.atguigu.service.TravelGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/TravelGroup")
public class TravelGroupController {
    @Reference
    TravelGroupService travelGroupService;

    @RequestMapping("/add")
    public Result add(@RequestBody TravelGroup travelGroup,Integer[] travelItemIds) {
        try {
            travelGroupService.add(travelGroup, travelItemIds);
            return new Result(true, MessageConstant.ADD_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_TRAVELGROUP_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return travelGroupService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());

    }

    @RequestMapping("/get")
    public Result get(Integer id){
        try {
            TravelGroup travelGroup = travelGroupService.get(id);
            return new Result(true, MessageConstant.QUERY_TRAVELGROUP_SUCCESS, travelGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

    @RequestMapping("/getTravelItemIds")
    public Result getTravelItemIds( Integer travelGroupId){
        try {
            List<Integer> travelItemIds = travelGroupService.getTravelItemIds(travelGroupId);
            return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,travelItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }

    }
    @RequestMapping("/edit")
    public Result edit(Integer[] travelItemIds,@RequestBody TravelGroup travelGroup) {
        try {
            travelGroupService.edit(travelItemIds,travelGroup);
            return new Result(true,MessageConstant.EDIT_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_TRAVELGROUP_FAIL);
        }

    }
  /*  @RequestMapping("/delete")
    public com.atguigu.entity.Result delete(Integer id) {
        try {
            travelGroupService.delete(id);
            return new com.atguigu.entity.Result(true,MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return new com.atguigu.entity.Result(false,MessageConstant.DELETE_TRAVELGROUP_FAIL);
        }
    }*/
  @RequestMapping("/findAll")
  public Result findAll() {
      try {
          List<TravelGroup> list=travelGroupService.findAll();
          return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS,list);
      } catch (Exception e) {
          e.printStackTrace();
          return new Result(true, MessageConstant.ADD_SETMEAL_FAIL);
      }
  }
}
